<script setup>
import { ref } from 'vue'
import {userLogin} from "@/api/login.js";
import {ElMessage} from "element-plus";
import router from "@/router/index.js";
import {useUserStore} from "@/stores/index.js";
const loginFormRef = ref()
const loginLoading = ref(false)
const loginForm = ref({
  email: '',
  password: ''
})

/**
 * 重置表单
 */
const resetForm = () => {
  loginFormRef.value.resetFields()
}

/**
 * 提交表单
 */
const submitForm = () => {
  if (!loginFormRef.value) return
  loginFormRef.value.validate((valid) => {
    if (valid) {
      loginLoading.value = true
      userLogin(loginForm.value).then((res) => {
        if (res.data.code === 1){
          ElMessage.success('登录成功')
          const userStore = useUserStore()
          userStore.setUserInfo(res.data.data)
          router.push('/')
        }
      }).catch(error => {
        console.log(error)
      }).finally(() => {
        loginLoading.value = false
      })
    } else {
      console.log('error submit!!')
    }
  })
}

/**
 * 校验规则
 */
const loginFormRules = {
  email: [
    { required: true, message: '请输入企业邮箱', trigger: 'blur' },
    // 添加邮箱格式的正则验证规则，简单的邮箱格式验证，可根据实际情况调整更严格的
    {
      pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
      message: '请输入正确的企业邮箱格式',
      trigger: 'blur'
    }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    // 添加密码格式的正则验证规则，要求密码是4-16位包含中英文
    {
      pattern: /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d\S]{4,16}$/,
      message: '密码需为4-16位包含英文字母和数字',
      trigger: 'blur'
    }
  ]
}
</script>

<template>
  <div class="container">
    <div class="login-card">
      <h2>云借阅图书管理系统</h2>
      <h4>CloudLibrary Management System</h4>
      <el-form ref="loginFormRef" :model="loginForm" class="login-form" :rules="loginFormRules">
        <el-form-item prop="email">
          <el-input
            type="text"
            placeholder="请输入企业邮箱"
            v-model="loginForm.email"
            prefix-icon="User"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
            v-model="loginForm.password"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            class="button-login"
            type="primary"
            @click="submitForm"
            :loading="loginLoading"
          >登录</el-button>
          <el-button
            class="button-reset"
            type="info"
            @click="resetForm"
          >重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style scoped>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: url("../../assets/img/manage-bg.png") no-repeat;
  background-size: 100% 100%;
}

.login-card {
  width: 300px;
  height: auto;
  background-color: #fff;
  border-radius: 5px;
  padding: 20px;
}

.login-form {
  margin-top: 20px;
}

.button-login, .button-reset {
  width: 140px;
}

.button-reset {
  margin-left: auto;
}
</style>
